﻿Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Linq
Imports System.Xml.Serialization
Imports System.Web

Namespace roncliProductions.LibWowArmory

    ''' <summary>
    ''' A class that retrieves a character's talents from the Armory
    ''' </summary>
    ''' <remarks></remarks>
    Public Class CharacterTalents
        Inherits WowArmoryXML

#Region "Declarations"

        Private ctPage As New CharacterTalentsSchema.page
        Private strRealm As String
        Private strName As String
        Private tTalents As Talents

#End Region

        ''' <summary>
        ''' The realm the character is on
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Realm() As String
            Get
                Return strRealm
            End Get
            Set(ByVal value As String)
                strRealm = value
            End Set
        End Property

        ''' <summary>
        ''' The name of the character
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Name() As String
            Get
                Return strName
            End Get
            Set(ByVal value As String)
                strName = value
            End Set
        End Property

        ''' <summary>
        ''' The URL that will be used to retrieve the character's talents from the Armory
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL() As String
            Get
                Return String.Format("http://www.wowarmory.com/character-talents.xml?r={0}&n={1}", strRealm, strName)
            End Get
        End Property

        ''' <summary>
        ''' The character's talents
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Talents() As Talents
            Get
                Return tTalents
            End Get
        End Property

        ''' <summary>
        ''' A constructor to load a character's talents by realm and character name
        ''' </summary>
        ''' <param name="strRealm"></param>
        ''' <param name="strName"></param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strRealm As String, ByVal strName As String)
            Me.strRealm = strRealm
            Me.strName = strName
            Load()
        End Sub

        ''' <summary>
        ''' Loads the character's talents from the Armory
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            Dim strKey As String = String.Format("LibWowArmory.CharacterTalents.{0}.{1}", strRealm, strName)
            If IsNothing(Cache.Cache(strKey)) Then
                MyBase.Retrieve()

                Dim xsSerializer As New XmlSerializer(ctPage.GetType())

                Dim srXML As New StringReader(MyBase.strXML)

                ctPage = CType(xsSerializer.Deserialize(srXML), CharacterTalentsSchema.page)

                Cache.Cache.Insert(strKey, ctPage, Nothing, Date.UtcNow.AddDays(1), Nothing, Caching.CacheItemPriority.Normal, Nothing)
            Else
                ctPage = CType(Cache.Cache(strKey), CharacterTalentsSchema.page)
            End If

            If IsNothing(ctPage) Then
                Throw New ArgumentException("The character you are trying to retrieve talents for does not exist.")
            End If
            If IsNothing(ctPage.characterInfo) Then
                Throw New ArgumentException("The character you are trying to retrieve talents for does not exist.")
            End If
            If IsNothing(ctPage.characterInfo.First().character) Then
                Throw New ArgumentException("The character you are trying to retrieve talents for does not exist.")
            End If
            If IsNothing(ctPage.characterInfo.First().talentTab) Then
                Throw New ArgumentException("The character you are trying to retrieve talents for does not exist.")
            End If

            tTalents = New Talents(ctPage.characterInfo.First().character.First().class)

            Dim strTalents = ctPage.characterInfo.First().talentTab.First().talentTree.First().value

            For intCount As Integer = 0 To strTalents.Length - 1
                tTalents.GetTalentByID(intCount).Points = CInt(strTalents.Chars(intCount).ToString())
            Next
        End Sub

    End Class

End Namespace